#include "GY_CategoryTreeSetValDlg.h"
#include "ui_GY_CategoryTreeSetValDlg.h"
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include <QJsonObject>
#include "GY_SelectTreeDialog.h"
GY_CategoryTreeSetValDlg::GY_CategoryTreeSetValDlg(QJsonArray columnsValue, QVector<QVector<QVariant>> nodes, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GY_CategoryTreeSetValDlg),
//    columnsValue(columnsValue),
    nodes(nodes)
{
    ui->setupUi(this);

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);


    for(QJsonValue value : columnsValue) {
        QJsonObject kv = value.toObject();
        if("id" == kv["column"].toString()){
            continue;
        }
        int row = ui->tableWidget->rowCount();
        ui->tableWidget->setRowCount(row + 1);

        if("pid" == kv["column"].toString()){
            QPushButton* btn = new QPushButton(ui->tableWidget);
            btn->setText(kv["column"].toString());
            connect(btn,&QPushButton::clicked,this,&GY_CategoryTreeSetValDlg::setPid);
            ui->tableWidget->setCellWidget(row, 0, btn);
            pidRow = row;

            oldPid = kv["value"].toString();
            if(oldPid.isEmpty()){
                oldPid = "0";
            }

            ui->tableWidget->setItem(row, 1, new QTableWidgetItem(oldPid));
        }
        else{
            if("name" == kv["column"].toString()){
                nameRow = row;
                oldName = kv["value"].toString();
            }
            ui->tableWidget->setItem(row, 0, new QTableWidgetItem(kv["column"].toString()));

            QLineEdit* lineEdit = new QLineEdit(ui->tableWidget);
            lineEdit->setText(kv["value"].toString());
            ui->tableWidget->setCellWidget(row, 1, lineEdit);
        }

    }

}


GY_CategoryTreeSetValDlg::~GY_CategoryTreeSetValDlg()
{
    delete ui;
}

void GY_CategoryTreeSetValDlg::setPid()
{

    GY_SelectTreeDialog tree(nodes, oldPid);
    if(QDialog::Accepted == tree.exec()){
        QString pid = QString("%1").arg(tree.getGroupId());
        ui->tableWidget->setItem(pidRow,1, new QTableWidgetItem(pid));
    }
}

void GY_CategoryTreeSetValDlg::on_pushButton_accept_clicked()
{
    QLineEdit* nameEdit = (QLineEdit*)ui->tableWidget->cellWidget(nameRow, 1);
    QString newName = nameEdit->text().trimmed();
    if(newName.isEmpty()){
        QMessageBox::information(this,"提示","name 不能为空");
        return;
    }

    //重名检查
    bool sameName = false;
    for (QVector<QVariant> row : nodes) {
        QString name = row[1].toString();
        if(oldName.isEmpty()){
            if(name == newName){
                sameName = true;
                break;
            }
        }
        else{
            if(oldName != newName){  //已改名
                if(name != oldName  //找到其他名
                    && name == newName)  //和其他名重名
                {
                    sameName = true;
                    break;
                }
            }
        }
    }
    if(sameName){
        QMessageBox::information(this,"提示","name 重名，请检查");
        return;
    }


    QJsonArray columns;
    for(int i = 0; i < ui->tableWidget->rowCount(); ++i){
        QJsonObject kv;
        if(i == pidRow){
            kv["column"] = "pid";
            kv["value"] = ui->tableWidget->item(i,1)->text();
        }
        else{
            QLineEdit* lineEdit = (QLineEdit*)ui->tableWidget->cellWidget(i, 1);
            QString strValue = lineEdit->text().trimmed();
            kv["column"] = ui->tableWidget->item(i,0)->text();
            kv["value"] = strValue;
        }
        columns.append(kv);
    }

    columnsValue = columns;

    accept();
}

QJsonArray GY_CategoryTreeSetValDlg::getColumnsValue() const
{
    return columnsValue;
}

void GY_CategoryTreeSetValDlg::on_pushButton_reject_clicked()
{
    reject();
}
